package com.baosight.sczx.zm.cn.service;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.zm.cn.domain.ZMCNCcy;
import com.baosight.sczx.zm.cn.domain.ZMCNCoalAccount;
import com.baosight.sczx.zm.cn.domain.ZMCNMzjc;

/**
 * <p>Title: CalculateStock.java</p>
 * <p>Description: 煤质化验结果接口</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author lhx
 * @version 1.0
 * <pre>Histroy:
 *          2016-10-22 22:09:01  李红星  Create
 *</pre>
 */
public class ServiceZMCN00 extends ServiceEPBase{
	/**
	 * 根据煤样编码带出其余相关信息
	 * <p>Function: queryCoalTestResult</p>
	 * <p>Description: 输入煤样编码，失去焦点时去数据库中根据 煤样编码查询出其他化验结果信息</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-10-22 21:57:10 李红星 create
	 * </p>
	 *
	 */
	public EiInfo queryCoalTestResult(EiInfo inInfo) {
		if(inInfo.get("row_index")!=null && Integer.parseInt(inInfo.get("row_index")+"")<0){
			//说明方法调用来自于按钮：单行或多行
			ZMCNCcy _dom = new ZMCNCcy();
			EiBlock eiBlock = inInfo.getBlock("result");
			eiBlock.setBlockMeta(_dom.eiMetadata);
			int rowcount = eiBlock.getRowCount();
			for (int i = 0; i < rowcount; i++) {
				_dom.fromMap(eiBlock.getRow(i));
				inInfo.set("fdMyNum", _dom.getFdMyNum());
				inInfo=validate(inInfo);
				if(inInfo.getStatus()==-1){
					break;
				}
			}
		}else{
			//说明方法调用来自于单元格失去焦点:单行
			inInfo=validate(inInfo);
		}
		return inInfo;
	}
	private EiInfo validate(EiInfo inInfo){
        StaticTools.setQueryParameters(inInfo, "fdMyNum", inInfo.get("fdMyNum"), true);  
		inInfo = super.query(inInfo, "ZMCNMzjc.querycoaltestresult", new ZMCNMzjc());
		if(inInfo.getBlock("result").getRowCount()<=0){
			inInfo = super.query(inInfo, "ZMCNCoalAccount.querycoaltestresult", new ZMCNCoalAccount());
			if(inInfo.getBlock("result").getRowCount()<=0){
				inInfo.setStatus(-1);
				inInfo.setMsg("您输入的煤样编码“"+inInfo.get("fdMyNum")+"”结果不存在，请检查是否正确");
			}
		}
		return inInfo;
	}
	
	/**
	 * 从煤样编码中截取录入日期
	 * @param mineCode String
	 * @return date String
	 */
    public String getDateFromMineCode(String mineCode){
    	String date="";
    	if(mineCode==null||mineCode.equals("")){
    		return StaticTools.getDate();
    	}
		if(mineCode.length()>=10){
			date=mineCode.substring(2,6)+"-"+mineCode.substring(6,8)+"-"+mineCode.substring(8,10);
			if(!StaticTools.isDate(date)){
				date="煤样编码错";
			}
		}else{
			date="煤样编码错";
		}
		return date;
    }
}
